package com.cloudbroker.bcs.csvc.base.common.util;

import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import com.cloudbroker.bcs.csvc.base.common.exception.HSBCCSException;
import org.apache.commons.lang3.StringUtils;

import com.cloudbroker.bcs.common.configure.constants.ConfigureConstants;
import com.cloudbroker.bcs.common.util.ValueUtil;
import com.cloudbroker.bcs.csvc.base.api.constants.HSBCCSErrorCodes;
import com.cloudbroker.bcs.csvc.base.api.constants.HSBCCSFields;

/**
 * 公共标记类，采用泛型控制
 */
public class HSBCCSServiceHelper {
    
    public static  <T extends Object>  void markError(Map<String, T> result, String errorNo, String errorInfo) {
        result.put(HSBCCSFields.ERROR_NO, (T)errorNo);
        result.put(HSBCCSFields.ERROR_INFO, (T)errorInfo);
    }
    
    public static <T extends Object> void markError(Map<String, T> result, HSBCCSException exception) {
        result.put(HSBCCSFields.ERROR_NO, (T)exception.getErrorNo());
        result.put(HSBCCSFields.ERROR_INFO, (T)exception.getErrorInfo());
    }
    
    public static <T extends Object> void markBizError(Map<String, T> result, String bizErrorNo, String bizErrorInfo) {
        result.put(HSBCCSFields.BIZ_ERROR_NO,(T) bizErrorNo);
        result.put(HSBCCSFields.BIZ_ERROR_INFO,(T) bizErrorInfo);
    }
    
    public static <T extends Object> void markSuccess(Map<String, T> result) {
        markError(result, HSBCCSErrorCodes.SUCCESS, "Operation success.");
    }
    
    public static <T extends Object> void markSuccess(Map<String, T> result, String errorInfo) {
        markError(result, HSBCCSErrorCodes.SUCCESS, errorInfo);
    }
    
    public static <T extends Object> void markCommonError(Map<String, T> result) {
        markError(result, HSBCCSErrorCodes.COMMON_ERROR, "Operation failed.");
    }
    
    public static <T extends Object> void markCommonError(Map<String, T> result, String errorInfo) {
        markError(result, HSBCCSErrorCodes.COMMON_ERROR, errorInfo);
    }
    
    public static <T extends Object> void markNoSuchImpl(Map<String, T> result) {
        markError(result, HSBCCSErrorCodes.NO_SUCH_IMPL, "No such service implement.");
    }
    
    public static <T extends Object> void markParamError(Map<String, T> result, String errorInfo) {
        markError(result, HSBCCSErrorCodes.PARAM_ERROR, errorInfo);
    }
    
    public static <T extends Object> void markRemoteServiceAccessError(Map<String, T> result, String errorInfo) {
        markError(result, HSBCCSErrorCodes.REMOTE_SERVICE_ACCESS_ERROR, errorInfo);
    }
    
    public static <T extends Object> void markRemoteServiceBizError(Map<String, T> result, String errorInfo) {
        markError(result, HSBCCSErrorCodes.REMOTE_SERVICE_BIZ_ERROR, errorInfo);
    }
    
    public static <T extends Object> void markRemoteServiceBizError(Map<String, T> result, String errorInfo, String bizErrorNo,
            String bizErrorInfo) {
        markError(result, HSBCCSErrorCodes.REMOTE_SERVICE_BIZ_ERROR, errorInfo);
        markBizError(result, bizErrorNo, bizErrorInfo);
    }
    
    public static <T extends Object> void markUnsupportedOperation(Map<String, T> result) {
        markError(result, HSBCCSErrorCodes.UNSUPPORTED_OPERATION, "Unsupported operation.");
    }
    
    public static <T extends Object> void markRemoteResponseInvalid(Map<String, T> result, String errorInfo) {
        markError(result, HSBCCSErrorCodes.REMOTE_RESPONSE_INVALID, errorInfo);
    }
    
    public static <T extends Object> void clearConfigParam(Map<String, T> param) {
        Iterator<Entry<String, T>> entryIt = param.entrySet().iterator();
        while (entryIt.hasNext()) {
            Entry<String, T> entry = entryIt.next();
            if (entry.getKey().startsWith(ConfigureConstants.CONFIG_PARAM_PREFIX)) {
                entryIt.remove();
            }
        }
    }
    
    public static  <T extends Object> Object getParamRequired(Map<String, T> param, String key) {
        Object value = param.get(key);
        if (null == value || (value instanceof String) && StringUtils.isBlank((String) value)
                || ValueUtil.isEmpty(value)) {
            throw new HSBCCSException(HSBCCSErrorCodes.PARAM_ERROR, key + " is required");
        }
        return value;
    }
    
    public static <T extends Object> Object removeParamRequired(Map<String, T> param, String key) {
        Object value = param.remove(key);
        if (null == value || (value instanceof String) && StringUtils.isBlank((String) value)
                || ValueUtil.isEmpty(value)) {
            throw new HSBCCSException(HSBCCSErrorCodes.PARAM_ERROR, key + " is required");
        }
        return value;
    }
    
    public static <T extends Object> String getParamRequiredAsString(Map<String, T> param, String key) {
        Object value = param.get(key);
        if (null == value || (value instanceof String) && StringUtils.isBlank((String) value)
                || ValueUtil.isEmpty(value)) {
            throw new HSBCCSException(HSBCCSErrorCodes.PARAM_ERROR, key + " is required");
        }
        return ValueUtil.getString(value);
    }
    
    public static <T extends Object> String removeParamRequiredAsString(Map<String, T> param, String key) {
        Object value = param.remove(key);
        if (null == value || (value instanceof String) && StringUtils.isBlank((String) value)
                || ValueUtil.isEmpty(value)) {
            throw new HSBCCSException(HSBCCSErrorCodes.PARAM_ERROR, key + " is required");
        }
        return ValueUtil.getString(value);
    }

    public static <T extends Object> String removeParamWithoutRequired(Map<String, T> param, String key) {
        Object value = param.remove(key);
        if (null == value || (value instanceof String) && StringUtils.isBlank((String) value)
                || ValueUtil.isEmpty(value)) {
            return null;
        }
        return ValueUtil.getString(value);
    }
    
}
